package com.household.controller;

import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * Created By wanzhen
 * 2023. 2023/3/25. 11:50
 * 验证码
 */

@RestController
public class KapthcaController {
    private final Producer producer;

    @Autowired
    public KapthcaController(Producer producer) {
        this.producer = producer;
    }

    @RequestMapping("/vg.jpg")
    public void veriftCode(HttpSession session, HttpServletResponse response) throws IOException {
        String text = producer.createText();
        session.setAttribute("kaptcha",text);
        BufferedImage image = producer.createImage(text);
        response.setContentType(MediaType.IMAGE_PNG_VALUE);
        ServletOutputStream outputStream = response.getOutputStream();
        ImageIO.write(image,"jpg",outputStream);


    }
}
